package com.itheima.springbootwebtlias.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.itheima.springbootwebtlias.annotation.LogOperation;
import com.itheima.springbootwebtlias.annotation.LoginLog;
import com.itheima.springbootwebtlias.annotation.MyLog;
import com.itheima.springbootwebtlias.exception.PasswordNotMatchException;
import com.itheima.springbootwebtlias.exception.UsernameNotExitsException;
import com.itheima.springbootwebtlias.mapper.EmpExprMapper;
import com.itheima.springbootwebtlias.mapper.EmpLogMapper;
import com.itheima.springbootwebtlias.mapper.EmpMapper;
import com.itheima.springbootwebtlias.pojo.*;
import com.itheima.springbootwebtlias.service.DeptService;
import com.itheima.springbootwebtlias.service.EmpExprService;
import com.itheima.springbootwebtlias.service.EmpLogService;
import com.itheima.springbootwebtlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private DeptService deptService;

    @Autowired
    private EmpExprService empExprService;



    @Override
    public PageResult pageQuery(EmpQueryParam param) {
        //1.构建分页对象
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());

        //2.调用service层方法分页查询
        //2.1 动态的构建多条件
        LambdaQueryWrapper<Emp> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(param.getName() != null && !param.getName().isEmpty(),Emp::getName, param.getName())
                .eq(param.getGender() != null,Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate, param.getBegin(), param.getEnd());

        //2.2 分页查询
        page = this.page(page,wrapper);

        // 3.查询部门列表
        List<Dept> depts = deptService.list();

        List<Emp> emps = page.getRecords();

        // 4.设置部门名称
        emps.forEach(emp -> {
            depts.forEach(dept -> {
                if (dept.getId().equals(emp.getDeptId())) {
                    emp.setDeptName(dept.getName());
                }
            });
        });

        return new PageResult<>(page.getTotal(),emps);
    }

    // 添加员工
    @Override
    public void saveEmp(Emp emp) {
        // 1.添加员工
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        this.save(emp);

        // 2。添加员工工作经历
        List<EmpExpr> exprList = emp.getExprList();
        exprList.stream().forEach(expr -> {expr.setEmpId(emp.getId());});

        empExprService.saveBatch(exprList);

    }

    // 根据id查询员工信息
    @Override
    public Emp getEmpById(Integer id) {
        // 1.查询员工表
        Emp emp = this.getById(id);

        // 2.查询工作经历表
        emp.setExprList(empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id)));

        return emp;
    }

    // 修改员工信息
    @Override
    public void updateBy(Emp emp) {
        // 1.更新员工表
        emp.setUpdateTime(LocalDateTime.now());
        this.updateById(emp);

        // 2.删除员工工作经历表
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));

        // 3.添加员工工作经历表
        emp.getExprList().stream().forEach(expr -> {expr.setEmpId(emp.getId());});
        empExprService.saveBatch(emp.getExprList());

    }

    // 批量删除员工
    @Override
    public void removeByIdss(List<Integer> ids) {
        // 1.删除员工表
        this.removeByIds(ids);

        // 2.删除员工工作经历表
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));

    }


    // 登录
    @Override
    public Emp checkLogin(Emp emp) {
        // 1.调用持久层EmpMapper的方法，根据用户名和密码查询员工信息
        Emp resultEmp = empMapper.selectByUsernameAndPassword(emp.getUsername());

        // 2.判断员工是否存在，如果不存在，则抛出异常
        if(resultEmp == null){
            // 业务层异常
            throw new UsernameNotExitsException("请睁大你的狗眼看清楚");
        }

        // 3.如果存在，则比较密码，如果密码不匹配，则抛出异常
        if(!resultEmp.getPassword().equals(emp.getPassword())){
            // 业务层异常
            throw new PasswordNotMatchException("请睁大你的狗眼看清楚，密码不对");
        }

        return resultEmp;
    }
}
